package chapter11.section4;
/* 程序11-2 QueueMachine：增加了线程通信的排队机 */

import java.util.LinkedList;

public class QueueMachine {
    private final LinkedList<Transaction> queue = new LinkedList<Transaction>();

    public boolean isEmpty() {
        return queue.isEmpty();
    }

    //银行职员线程调用get方法，从排队机获得一项业务
    public synchronized Transaction get() throws InterruptedException {
        while (queue.isEmpty()) {
            this.wait();
        }
        Transaction tran = queue.poll();
        return tran;
    }

    //业务发生器线程调用add方法，向排队机增加一项待处理业务
    public synchronized void add(Transaction tran) {
        this.queue.add(tran);
        this.notifyAll();
    }
}
